package com.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResourceInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handle) throws ServletException, IOException {
        //如果用户登录放行
        Object user=request.getSession().getAttribute("USER_SESSION");
        if(user!=null){
            return true;
        }
        //用户没登录，判断是否访问的是登录功能，如果是放行
        String requestURI=request.getRequestURI();//获取用户页面url
        if(requestURI.indexOf("login")>0){//判断是否包含login
            return true;
        }
        //如果不是登录功能，需跳转到登录页面
        request.setAttribute("msg","还没登录啊！！！");
        request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        return false;
    }
}
